package com.xiaomage.view.animation10

import android.animation.AnimatorInflater
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.AnimationSet
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bt_xml_animator.setOnClickListener{
            val xmlValueAnimator=AnimatorInflater.loadAnimator(this,
                    R.animator.animator) as ValueAnimator
            xmlValueAnimator.addUpdateListener {
                animator->
                view.layout(animator.animatedValue.toString().toInt(),
                        animator.animatedValue.toString().toInt(),
                        animator.animatedValue.toString().toInt()+view.width,
                        animator.animatedValue.toString().toInt()+view.height)
            }
            xmlValueAnimator.start()
        }
        bt_xml_object_animator.setOnClickListener{
            val xmlObjectAnimator=AnimatorInflater.loadAnimator(this,
                    R.animator.object_animator_translation) as ObjectAnimator
            xmlObjectAnimator.target=view
            xmlObjectAnimator.start()
        }
        bt_xml_object_animator_color.setOnClickListener{
            val xmlobjectAnimator=AnimatorInflater.loadAnimator(this,
                    R.animator.object_animator_color) as ObjectAnimator
            xmlobjectAnimator.target=view
            xmlobjectAnimator.start()
        }

        bt_xml_animator_set.setOnClickListener{
            val setAnimator=AnimatorInflater.loadAnimator(this,
                    R.animator.animator_set) as AnimatorSet
            setAnimator.setTarget(view)
            setAnimator.start()
        }
        bt.setOnClickListener{
            onClick(0)
        }
        bt1.setOnClickListener{
            onClick(1)
        }
        bt2.setOnClickListener{
            onClick(2)
        }
        bt3.setOnClickListener{
            onClick(3)
        }
        bt4.setOnClickListener{
            onClick(4)
        }
        bt5.setOnClickListener{
            onClick(5)
        }
        bt6.setOnClickListener{
            onClick(6)
        }
    }
    //判读是否已经打开
    var isOpen=false
    var max=6
    var radius=300
    fun onClick(index:Int){
        Log.e("点击了",""+index)
        if(index==0){
            if(isOpen){
                Log.e("点击了","1")
                isOpen=!isOpen
                close(1,bt1)
                close(2,bt2)
                close(3,bt3)
                close(4,bt4)
                close(5,bt5)
                close(6,bt6)
            }else{
                Log.e("点击了","2")
                isOpen=!isOpen
                open(1,bt1)
                open(2,bt2)
                open(3,bt3)
                open(4,bt4)
                open(5,bt5)
                open(6,bt6)
            }
        }else{
            Toast.makeText(this,"你点击了"+index,Toast.LENGTH_LONG).show()
        }

    }
    fun open(index:Int,view: View){
        //每个漂移角度
        val degree: Int =(index-1)*(90/5)
        Log.e("点击了","opendegree"+degree)
        val translationX:Float=-radius*Math.sin(Math.toRadians(degree.toDouble())).toFloat()
        Log.e("点击了translationX",""+translationX)
        val translationY:Float=-radius*Math.cos(Math.toRadians(degree.toDouble())).toFloat()
        Log.e("点击了translationY",""+translationY)
        val setAnimator=AnimatorSet()
        setAnimator.playTogether(
                ObjectAnimator.ofFloat(view,"translationX",0f,translationX),
                ObjectAnimator.ofFloat(view,"translationY",0f,translationY),
                ObjectAnimator.ofFloat(view,"scaleX",0f,1f),
                ObjectAnimator.ofFloat(view,"scaleY",0f,1f),
                ObjectAnimator.ofFloat(view,"alpha",0f,1f)
        )
        setAnimator.duration=500
        setAnimator.start()

    }
    fun close(index:Int,view: View){
        //每个漂移角度
        val degree: Int =(index-1)*(90/5)
        val translationX:Float=-radius*Math.sin(Math.toRadians(degree.toDouble())).toFloat()
        val translationY:Float=-radius*Math.cos(Math.toRadians(degree.toDouble())).toFloat()
        val setAnimator=AnimatorSet()
        setAnimator.playTogether(
                ObjectAnimator.ofFloat(view,"translationX",translationX,0f),
                ObjectAnimator.ofFloat(view,"translationY",translationY,0f),
                ObjectAnimator.ofFloat(view,"scaleX",1f,0f),
                ObjectAnimator.ofFloat(view,"scaleY",1f,0f),
                ObjectAnimator.ofFloat(view,"alpha",1f,0f)
        )
        setAnimator.duration=500
        setAnimator.start()
    }

}
